home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / toolkit / db2stff / tipwnd.c < prev   
Encoding:
C/C++ Source or Header  |  1995-03-28  |  4.1 KB  |  161 lines

  1. //
  2. // TIPWND.C
  3. //
  4. // This is linked with DBTTIP.VBX. It contains all the code managing
  5. // the tool tip window.
  6. //
  7. // "Real programmers don't write comments - the code is obvious"
  8. //
  9.  
  10. #include "dbttip.h"
  11.  
  12.  
  13. void PaintTheTip( HDC hDC, LPRECT lpRect, HCTL hctl)
  14. {
  15.  LPDBTTIP lpDBTTip = LPDBTTIPDEREF( hctl );
  16.  PIC pic;
  17.  
  18.  VBGetPic( lpDBTTip->uUse.hPic, &pic );
  19.  lpDBTTip = LPDBTTIPDEREF( hctl );
  20.  if ( pic.picType == PICTYPE_METAFILE )
  21.   {  //Draw the WMF
  22.    short sdc = SaveDC( hDC );
  23.    SetMapMode( hDC, MM_ANISOTROPIC );
  24.    SetWindowExt( hDC, pic.picData.wmf.xExt, pic.picData.wmf.yExt );
  25.    SetViewportExt( hDC, lpRect->right, lpRect->bottom );
  26.    PlayMetaFile( hDC, pic.picData.wmf.hmeta );
  27.    RestoreDC( hDC, sdc );
  28.   }
  29.  else
  30.    return;  // Error - no picture
  31.  
  32.  { // Draw the text
  33.   HFONT hFontOld = NULL;
  34.   UINT uAlign;
  35.  
  36.   switch ( lpDBTTip->uUse.nAlignment )
  37.    {
  38.     case 0: uAlign = DT_LEFT; break;
  39.     case 1: uAlign = DT_RIGHT; break;
  40.     case 2: uAlign = DT_CENTER; break;
  41.    }
  42.   if ( lpDBTTip->uUse.hFont )
  43.     hFontOld = SelectObject( hDC, lpDBTTip->uUse.hFont );
  44.   SetBkMode( hDC, TRANSPARENT );
  45.   SetTextColor( hDC, lpDBTTip->uUse.rgbForeColor );
  46.   DrawText(hDC, lpDBTTip->szText, -1, &lpDBTTip->rectText, uAlign|DT_WORDBREAK );
  47.   if ( hFontOld )
  48.      SelectObject( hDC, hFontOld );
  49.  }
  50.  
  51. }
  52.  
  53.  
  54. /////////////////////////////////////////////////////////////////////////////
  55. // A normal window procedure
  56.  
  57. long FAR PASCAL __export TipWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  58. {
  59.  switch ( message )
  60.   {
  61.    case WM_PAINT: {
  62.      PAINTSTRUCT ps;
  63.      RECT rect;
  64.      HDC hDC = BeginPaint( hWnd, &ps );
  65.      GetClientRect( hWnd, &rect );
  66.      PaintTheTip( hDC, &rect, (HCTL)GetWindowLong(hWnd,0) );
  67.      EndPaint( hWnd, &ps );
  68.      return 0; }
  69.   }
  70.  
  71.  return DefWindowProc(hWnd, message, wParam, lParam);
  72. }
  73.  
  74.  
  75. /////////////////////////////////////////////////////////////////////////////
  76. // Tool tip window creation and destruction
  77.  
  78. static char const szClassName[] = "DBTTipClass";
  79.  
  80. void CreateTipWnd(HCTL hctl)
  81. {
  82.  LPDBTTIP lpDBTTip = LPDBTTIPDEREF( hctl );
  83.  short nHotX = (short)( ((LONG)lpDBTTip->nTipWidth*(LONG)lpDBTTip->uUse.nHotSpotX) / 100L );
  84.  short nHotY = (short)( ((LONG)lpDBTTip->nTipHeight*(LONG)lpDBTTip->uUse.nHotSpotY) / 100L );
  85.  short nPosX = lpDBTTip->pMouse.x + lpDBTTip->uUse.nMouseOffX + lpDBTTip->nExtraOffX - nHotX;
  86.  short nPosY = lpDBTTip->pMouse.y + lpDBTTip->uUse.nMouseOffY + lpDBTTip->nExtraOffY - nHotY;
  87.  
  88.  // Posistion the tip so that it's always visible
  89.  if ( nPosX<0 )
  90.    nPosX = 0;
  91.  if ( nPosY<0 )
  92.    nPosY = 0;
  93.  if ( (nPosX+lpDBTTip->nTipWidth) > GetSystemMetrics(SM_CXSCREEN) )
  94.    nPosX = GetSystemMetrics(SM_CXSCREEN)-lpDBTTip->nTipWidth;
  95.  if ( (nPosY+lpDBTTip->nTipHeight) > GetSystemMetrics(SM_CYSCREEN) )
  96.    nPosY = GetSystemMetrics(SM_CYSCREEN)-lpDBTTip->nTipHeight;
  97.  
  98.  lpDBTTip->hWndTip = CreateWindow(
  99.     szClassName,
  100.     NULL,
  101.     WS_POPUP,
  102.     nPosX,
  103.     nPosY,
  104.     lpDBTTip->nTipWidth,
  105.     lpDBTTip->nTipHeight,
  106.     NULL,
  107.     NULL,
  108.     ghmodDLL,
  109.     NULL );
  110.  SetWindowPos( lpDBTTip->hWndTip, HWND_TOP, 0, 0, 0, 0,
  111.                SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE );
  112.  SetWindowLong( lpDBTTip->hWndTip, 0, (LONG)hctl );
  113. }
  114.  
  115.  
  116. void DestroyTipWnd(HCTL hctl)
  117. {
  118.  LPDBTTIP lpDBTTip = LPDBTTIPDEREF( hctl );
  119.  
  120.  if ( lpDBTTip->hWndTip )
  121.    DestroyWindow( lpDBTTip->hWndTip );
  122.  lpDBTTip->hWndTip = NULL;
  123.  lpDBTTip->nExtraOffX = 0;
  124.  lpDBTTip->nExtraOffY = 0;
  125. }
  126.  
  127.  
  128. /////////////////////////////////////////////////////////////////////////////
  129. // Tool tip class registration and unregistration
  130.  
  131. static short gnRegisters = 0;
  132.  
  133. BOOL RegisterTipClass(void)
  134. {
  135.  WNDCLASS wc;
  136.  
  137.  if ( gnRegisters++ )  // need only register once
  138.    return TRUE;
  139.  
  140.  wc.style            = NULL;
  141.  wc.lpfnWndProc        = TipWndProc;
  142.  wc.cbClsExtra        = 0;
  143.  wc.cbWndExtra        = sizeof(long);
  144.  wc.hInstance        = ghmodDLL;
  145.  wc.hIcon            = NULL;
  146.  wc.hCursor            = NULL;
  147.  wc.hbrBackground    = NULL;
  148.  wc.lpszMenuName    = NULL;
  149.  wc.lpszClassName    = szClassName;
  150.  
  151.  return RegisterClass( &wc );
  152. }
  153.  
  154.  
  155. void UnregisterTipClass(void)
  156. {
  157.  if ( gnRegisters-- )
  158.    return;
  159.  UnregisterClass( szClassName, ghmodDLL );
  160. }
  161.